package com.rs.yyds.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.rs.yyds.bean.MenuVo;
import com.rs.yyds.bean.User;
import com.rs.yyds.bean.UserVo;
import com.rs.yyds.common.BaseController;
import com.rs.yyds.common.Constants;
import com.rs.yyds.redis.RedisTemplateUtil;
import com.rs.yyds.service.SysConfigService;
import com.rs.yyds.service.UserService;
import com.rs.yyds.util.AjaxResult;
import com.rs.yyds.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Controller
@RequestMapping(value = "/loginController")
public class LoginController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplateUtil redisTemplateUtil;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 登录
     * @param user POJO方式的请求参数
     * 请求映射的方式，POST提请求，地址为login
     * 1、获取用户名密码
     * 2、判用户名密码是否为空
     * 3、将密码进行MD5加密
     * 4、通过用户名密码进行用户查询以及所属菜单查询
     * 5、将用户信息以及菜单信息存入到redis中去
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public AjaxResult login(@RequestBody User user){
        Map<String,Object> userMap = userService.findUserByEmailAndPwd(user);
        int code = (Integer) userMap.get("code");
        if( code != 0 ){
            request.setAttribute("msg",userMap.get("msg"));
            return AjaxResult.failure((String)userMap.get("msg"));
        }
//        登录成功，将登录用户信息以及菜单信息存入redis中
        redisTemplateUtil.set("User", JSONArray.toJSONString(userMap.get("User")));
        redisTemplateUtil.set("Menu", JSONArray.toJSONString(userMap.get("MenuList")));

        User us = (User)userMap.get("User");

//        SessionUtil.setUser(us);

        return AjaxResult.success(us.getUserName());

    }

    /**
     * 登录成功，跳转到首页
     * 获取用户的菜单
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView modelAndView=new ModelAndView();
//        redis中读取用户所属菜单
        String menus=(String)redisTemplateUtil.get("Menu");
        List<MenuVo> menuVoList =JSONObject.parseArray(menus,MenuVo.class);

//        将菜单数据返回到html页面中
        modelAndView.addObject("menusVo",menuVoList);
//        返回html页面
        modelAndView.setViewName("success");
        return modelAndView;
    }
    @PostMapping("/getMenu")
    public AjaxResult getMenu() {
//        redis中读取用户所属菜单
        String menus=(String)redisTemplateUtil.get("Menu");
        List<MenuVo> menuVoList =JSONObject.parseArray(menus,MenuVo.class);
        return AjaxResult.success(menuVoList);
    }
    /**
     * 修改密码
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    public AjaxResult updatePwd(@RequestBody UserVo user) {
//        从redis中读取用户的密码
        String str = redisTemplateUtil.get("User").toString();
        User us=JSONObject.parseObject(str,User.class);
        String pwd = us.getUserPassword();
//        判断初始密码与redis中存储的密码是否一致
        String pwdChu=MD5Util.getMD5Code(user.getUserPassword());
        if(!pwd.equals(pwdChu)){
            return AjaxResult.failure("初始密码错误");
        }
//        将修改后的新密码存入数据库
        String newPwd=MD5Util.getMD5Code(user.getNewUserPassword());
        us.setUserPassword(newPwd);
        int i = userService.updateUserById(us);
        if(i<=0){
            return AjaxResult.failure("密码修改失败");
        }
//        清空redis，用户退出，重新登录
        redisTemplateUtil.delete("User");
        redisTemplateUtil.delete("Menu");
//        request.getSession().invalidate();
        return AjaxResult.success();
    }
    /**
     * 用户退出
     * @return
     */
    @ResponseBody
    @RequestMapping("logout")
    public AjaxResult logout(HttpServletRequest request) {
//        request.getSession().invalidate();
//        清空redis，用户退出，重新登录
        redisTemplateUtil.delete("User");
        redisTemplateUtil.delete("Menu");
        return AjaxResult.success();
    }

}
